
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using BoOnce.Components;
using BoOnce.GameLogic;
using BoOnce.GameLogic.Entities;
using BoOnce.Graphics;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BoOnce.Xml;
using BoOnce.GameLogic.Entities.Drawable.Scenery;
using BoOnce.GameLogic.Entities.Drawable.GFX;
using BoOnce.Physic;
using BoOnce.Core;



namespace BoOnce.Steps
{
    class BusLevel : Step
    {
        Background[] _Backgrounds;
        BusPlayer _Player;
        CameraComponent _Camera;
        PhysicComponent _PhysicComponent;
        PlayerActionComponent _PAC;
        Bus _Bus;
        List<PhysicalScenery> _PhysicalSceneries;

        ParticulesEngine _ParticulesEngine;

        public BusLevel(Game game)
            : base(game, "Bus level")
        {
            _Camera = (CameraComponent)Game.Services.GetService(typeof(CameraComponent));

            PhysicComponentConfigXml PhysicConfig = new PhysicComponentConfigXml();
            PhysicConfig.Gravity = new Vector2(0,0);
            PhysicConfig.GroundHeight = 1;
            PhysicConfig.MultiSimulator = false;
            PhysicConfig.PhysicalEntityMaxPosX = Settings.SCREEN_X;
            PhysicConfig.PhysicalEntityMaxPosY = Settings.SCREEN_Y;
            PhysicConfig.PhysicalEntityMinPosX = 0;
            PhysicConfig.PhysicalEntityMinPosY = -1000;
            _PhysicComponent = new PhysicComponent(Game, PhysicConfig);
            _PhysicComponent.Enabled = true;
            Game.Components.Add(_PhysicComponent);
            _PAC = (PlayerActionComponent)Game.Services.GetService(typeof(PlayerActionComponent));
            _PAC.ClearPlayers();

            _ParticulesEngine = new ParticulesEngine(game, new ParticulesConfig());
            _ParticulesEngine.InitialForce = new Vector2(0f, 5000f);
            _ParticulesEngine.InitialForceNoise = new Vector2(1000f, 2000f);

            _PhysicalSceneries = new List<PhysicalScenery>();
        }

        public override void LoadContent()
        {
            _Player = new BusPlayer(PlayerIndex.One,new Vector2(350, 400),"BusHero",100,100,1);
            _PAC.AddPlayer(_Player);

            _Bus = new Bus(Game.Content.Load<Texture2D>(Settings.CONTENT_LEVEL_PATH + "/bus/bus"), new Vector2(100, 200),100f);

            _Backgrounds = new Background[2];
            _Backgrounds[0] = new Background(Game.Content.Load<Texture2D>(Settings.CONTENT_LEVEL_PATH + "/bus/ground1"),BackgroundBehaviour.STAY,1.0f);
            _Backgrounds[1] = new Background(Game.Content.Load<Texture2D>(Settings.CONTENT_LEVEL_PATH + "/bus/ground2"), BackgroundBehaviour.STAY,1.0f);

            int bgHeight = 0;
            foreach (Background bg in _Backgrounds)
            {
                bgHeight += (int)bg.Height;
            }

            // Camera configuration
            _Camera.SetCameraLimit(0, 0, -1000, 500);
            _Camera.FollowedEntity = _Player;


            _PhysicalSceneries.Add(new Egout(new Vector2(400, 500), _Player));
            _PhysicalSceneries.Add(new Egout(new Vector2(300, 300), _Player));
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            Game.Services.RemoveService(typeof(PhysicComponent));
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            int bgHeight = 0;
            foreach (Background bg in _Backgrounds)
            {
                bg.Position = new Vector2(0, -bgHeight);
                bgHeight += (int)bg.Height;
                bg.DrawedOrder = 1;
                bg.DEC.Add(bg);
            }
            _ParticulesEngine.Update(gameTime);

            _Player.Update(gameTime);
            _Player.DrawedOrder = 10;
            _Player.DEC.Add(_Player);

            _Bus.Update(gameTime);
            _Bus.DrawedOrder = 11;
            _Bus.DEC.Add(_Bus);

            _ParticulesEngine.Source.X = _Bus.Position.X;
            _ParticulesEngine.Source.Y = _Bus.Position.Y + _Bus.Height / 2.0f;

            foreach (PhysicalScenery scenery in _PhysicalSceneries)
            {
                scenery.Update(gameTime);
                scenery.DrawedOrder = 12;
                scenery.DEC.Add(scenery);
            }
            base.Update(gameTime);
        }
    }
}
